<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 *
 *
 * @package     h
 * @subpackage  models
 * @category    models
 * @author      hyw
 * @copyright   copyright (c) 2012
 * @filesource
 */
class Th_model extends Base_model {

    
	
    function get_demand_fields() {

	   return $this->db->list_fields('h_demand');

    }

    function get_object_fields() {

	   return $this->db->list_fields('h_object');

    }

    function get_client() {

    	$c=$this->db->get("h_client");
    	$p=array();

    	foreach ($c->result() as $value) {
    		if(!empty($value->name)){
				$p[$value->client_id] = $value->name;
			}
    	}

    	return $p;
    }



    function insert_demand($data) {

    		$this->db->insert('h_demand',$data);    	

    }
	
	
	
	function update_o_m_d() {
		
    			
		$clients_data=$this->get_client();			
		$q1=$this->db->get('h_object');	
		foreach($q1->result() as $row){
									
			$this->db->where( 	'object_id'	,	$row->object_id );
			$this->db->update(	'h_object'	, 	array('owner'=>isset($clients_data[$row->client_id]) ?$clients_data[$row->client_id]: ''));			
		}
		
		$q2=$this->db->get('h_demand');						
		foreach($q2->result() as $row){
									
			$this->db->where( 	'demand_id'	,	$row->demand_id );
			$this->db->update(	'h_demand'	, 	array('name'=>isset($clients_data[$row->client_id]) ?$clients_data[$row->client_id]: ''));			
		}

		$q3=$this->db->get('h_market_research');						
		foreach($q3->result() as $row){
									
			$this->db->where( 	'market_research_id'	,	$row->market_research_id );
			$this->db->update(	'h_market_research'	, 	array('owner'=>isset($clients_data[$row->client_id]) ?$clients_data[$row->client_id]: ''));			
		}
			    
			  	

    }

    function insert_object($data) {

    		$this->db->insert('h_object',$data);    	

    }

    function del_demand() {

    	$this->db->where('client_id', 0);
		$this->db->delete('h_demand'); 

    }

    function del_object() {

    	$this->db->where('client_id', 0);
    	//$this->db->where('client_id', 0);
		$this->db->delete('h_object'); 

    }



	
	
}